{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.4 使用列表的一部分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.4.1切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "player=['charles','martina','michael','florence','eli']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['charles', 'martina', 'michael']\n"
     ]
    }
   ],
   "source": [
    "print(player[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['martina', 'michael', 'florence']\n"
     ]
    }
   ],
   "source": [
    "print(player[1:4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['charles', 'martina']\n"
     ]
    }
   ],
   "source": [
    "print(player[:-3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.4.2 遍历切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Charles\n",
      "Martina\n",
      "Michael\n"
     ]
    }
   ],
   "source": [
    "for player in player[:3]:\n",
    "    print(player.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.4.3 复制列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My favorite foods are:\n",
      "['pizza', 'falafel', 'carrot cale']\n",
      "My fiend's favorite fodds are:\n",
      "['pizza', 'falafel', 'carrot cale']\n"
     ]
    }
   ],
   "source": [
    "my_foods=['pizza','falafel','carrot cale']\n",
    "friend_foods=my_foods[:]\n",
    "print(\"My favorite foods are:\")\n",
    "print(my_foods)\n",
    "\n",
    "print(\"My fiend's favorite fodds are:\")\n",
    "print(friend_foods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My favorite foods are:\n",
      "['pizza', 'falafel', 'carrot cale', 'cannoli']\n",
      "My fiend's favorite fodds are:\n",
      "['pizza', 'falafel', 'carrot cale', 'ice cream']\n"
     ]
    }
   ],
   "source": [
    "my_foods.append('cannoli')\n",
    "friend_foods.append('ice cream')\n",
    "print(\"My favorite foods are:\")\n",
    "print(my_foods)\n",
    "\n",
    "print(\"My fiend's favorite fodds are:\")\n",
    "print(friend_foods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My favorite foods are:\n",
      "['pizza', 'falafel', 'carrot cale', 'cannoli', 'ice cream']\n",
      "My fiend's favorite fodds are:\n",
      "['pizza', 'falafel', 'carrot cale', 'cannoli', 'ice cream']\n"
     ]
    }
   ],
   "source": [
    "my_foods=['pizza','falafel','carrot cale']\n",
    "friend_foods=my_foods#这行不通\n",
    "my_foods.append('cannoli')\n",
    "friend_foods.append('ice cream')\n",
    "print(\"My favorite foods are:\")\n",
    "print(my_foods)\n",
    "\n",
    "print(\"My fiend's favorite fodds are:\")\n",
    "print(friend_foods)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.5元组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.5.1 定义元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "dimensions=(200,50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    }
   ],
   "source": [
    "print(dimensions[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n"
     ]
    }
   ],
   "source": [
    "print(dimensions[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-006133b57ed9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdimensions\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m250\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "dimensions[0]=250"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "50\n"
     ]
    }
   ],
   "source": [
    "for dimension in dimensions:\n",
    "    print(dimension)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.6设置代码格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5 if语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.1 一个简单的示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Audi\n",
      "BMW\n",
      "Subaru\n",
      "Toyota\n"
     ]
    }
   ],
   "source": [
    "cars=['audi','bmw','subaru','toyota']\n",
    "for car in cars:\n",
    "    if car=='bmw':\n",
    "        print(car.upper())\n",
    "    else:\n",
    "        print(car.title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.2 条件测试"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2.1 检查是否相等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car='bmw'\n",
    "car=='bmw'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car='audi'\n",
    "car=='bmw'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2.2 检查是否相等时不考虑大小写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car=\"Audi\"\n",
    "car ==  'audi'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car='Audi'\n",
    "car.lower()=='audi'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Audi'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "car"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2.3 检查是否不相等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hold the anchovies!\n"
     ]
    }
   ],
   "source": [
    "requested_topping = 'mushrooms'\n",
    "if requested_topping != 'anchovies':\n",
    "    print('Hold the anchovies!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2.4 比较数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age=18\n",
    "age == 18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "That is not the correct answer. Please try agian!\n"
     ]
    }
   ],
   "source": [
    "answer = 17\n",
    "if answer != 42:\n",
    "    print('That is not the correct answer. Please try agian!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2.5 检查多个条件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_0=22\n",
    "age_1=18\n",
    "age_0>=21 and age_1 >=21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_1=23\n",
    "age_0>=21 and age_1>=21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_0=22\n",
    "age_1=18\n",
    "age_0>=21 or age_1>=21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_0 =18\n",
    "age_0>=21 or age_1>=21"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2.6 检查特定值是否包含在列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "requested_toppings=['mushrooms','onions','pineapple']\n",
    "'mushrooms' in requested_toppings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'pepproni' in requested_toppings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2.7 检查特定值是否不包含在列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marie, you can post a response if you wish.\n"
     ]
    }
   ],
   "source": [
    "banned_users=['andrew','caroline','david']\n",
    "user='marie'\n",
    "if user not in banned_users:\n",
    "    print(user.title()+', you can post a response if you wish.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.2.8 布尔表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "game_active=True\n",
    "can_edit=False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.3 if语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.3.1 简单的if语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are old enough to vote!\n"
     ]
    }
   ],
   "source": [
    "age=19\n",
    "if age>=18:\n",
    "    print('You are old enough to vote!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.3.2 if-else 语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sorry, you are not enough to vote.\n"
     ]
    }
   ],
   "source": [
    "age=17\n",
    "if age>=18:\n",
    "    print('You are old enough to vote')\n",
    "else:\n",
    "    print('Sorry, you are not enough to vote.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.3.3 if-elif-else 结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You admission cost is $5.\n"
     ]
    }
   ],
   "source": [
    "age=12\n",
    "if age<4:\n",
    "    print('You admission cost is $0.')\n",
    "elif age<18:\n",
    "    print('You admission cost is $5.')\n",
    "else:\n",
    "    print('Your admission cost is $10.')\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "green\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "alien_0={'color':'green','points':5}\n",
    "print(alien_0['color'])\n",
    "print(alien_0['points'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 6.2.5 删除键值对\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'color': 'green', 'points': 5}\n",
      "{'color': 'green'}\n"
     ]
    }
   ],
   "source": [
    "alien_0={'color':'green','points':5}\n",
    "print(alien_0)\n",
    "del alien_0['points']\n",
    "print(alien_0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.3.1 遍历所有的键值对\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Key: username\n",
      "Value:efermi\n",
      "\n",
      "Key: first\n",
      "Value:enrico\n",
      "\n",
      "Key: last\n",
      "Value:fermi\n"
     ]
    }
   ],
   "source": [
    "user_0={\n",
    "    'username':'efermi',\n",
    "    'first':'enrico',\n",
    "    'last':'fermi',\n",
    "}\n",
    "\n",
    "for key,value in user_0.items():\n",
    "    print('\\nKey: '+key)\n",
    "    print('Value:'+value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 9 类\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "#9.1.1 创建类\n",
    "class Dog():\n",
    "    def __init__(self,name,age):\n",
    "        self.name=name\n",
    "        self.age=age\n",
    "        \n",
    "    def sit(self):\n",
    "        print(self.name.title()+'is now sitting')\n",
    "        \n",
    "    def roll_over(self):\n",
    "        print(self.name.title()+'roller over')\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My dog's name is Willie.\n",
      "My dog is 6 years old.\n"
     ]
    }
   ],
   "source": [
    "my_dog=Dog('willie',6)\n",
    "print(\"My dog's name is \" + my_dog.name.title()+'.')\n",
    "print(\"My dog is \"+str(my_dog.age)+\" years old.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016 Audi A4\n",
      "This car has 0 miles on it.\n"
     ]
    }
   ],
   "source": [
    "#9.2.1 Car类\n",
    "class Car():\n",
    "    def __init__(self,make,model,year):\n",
    "        self.make=make\n",
    "        self.model=model\n",
    "        self.year=year\n",
    "        self.odometer_reading=0\n",
    "        \n",
    "    def get_descriptive_name(self):\n",
    "        long_name=str(self.year)+' '+self.make+' '+self.model\n",
    "        return long_name.title()\n",
    "    \n",
    "    def read_odometer(self):\n",
    "        print('This car has '+str(self.odometer_reading)+' miles on it.')\n",
    "\n",
    "    def update_odometer(self,mileage):\n",
    "        if mileage>=self.odometer_reading:\n",
    "            self.odometer_reading=mileage\n",
    "        else:\n",
    "            print(\"You can't roll back an odometer!\")\n",
    "            \n",
    "    def increment_odometer(self,miles):\n",
    "        self.odometer_reading+=miles\n",
    "        \n",
    "        \n",
    "my_new_car=Car('audi','a4',2016)\n",
    "print(my_new_car.get_descriptive_name())\n",
    "my_new_car.read_odometer()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This car has 23 miles on it.\n"
     ]
    }
   ],
   "source": [
    "my_new_car.odometer_reading=23\n",
    "my_new_car.read_odometer()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This car has 23 miles on it.\n"
     ]
    }
   ],
   "source": [
    "my_new_car.update_odometer(23)\n",
    "my_new_car.read_odometer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2013 Subaru Outback\n",
      "This car has 23500 miles on it.\n",
      "This car has 23600 miles on it.\n"
     ]
    }
   ],
   "source": [
    "my_used_car=Car('subaru','outback',2013)\n",
    "print(my_used_car.get_descriptive_name())\n",
    "\n",
    "my_used_car.update_odometer(23500)\n",
    "my_used_car.read_odometer()\n",
    "\n",
    "my_used_car.increment_odometer(100)\n",
    "my_used_car.read_odometer()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "#9.3继承\n",
    "class Battery():\n",
    "    \n",
    "    def __init__(self,battery_size=70):\n",
    "        self.battery_size=battery_size\n",
    "        \n",
    "    def describe_battery(self):\n",
    "        print(\"This car has a \"+str(self.battery_size)+\"-kWH battery.\")\n",
    "        \n",
    "    def get_range(self):\n",
    "        if self.battery_size==70:\n",
    "            range=240\n",
    "        elif self.battery_size==85:\n",
    "            range=270\n",
    "            \n",
    "        message=\"This car can go approximately \"+str(range)\n",
    "        message+=\" miles on a full charge.\"\n",
    "        print(message)\n",
    "class ElectriCar(Car):\n",
    "    \n",
    "    def __init__(self,make,model,year):\n",
    "        super().__init__(make,model,year)\n",
    "        self.battery=Battery()\n",
    "        \n",
    "    def describe_battery(self):\n",
    "        print(\"This car has a \"+str(self.battery_size)+\"-kWH battery.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2016 Tesla Model S\n",
      "This car has a 70-kWH battery.\n",
      "This car can go approximately 240 miles on a full charge.\n"
     ]
    }
   ],
   "source": [
    "my_tesla=ElectriCar('tesla','model s',2016)\n",
    "print(my_tesla.get_descriptive_name())\n",
    "my_tesla.battery.describe_battery()\n",
    "my_tesla.battery.get_range()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10 文件和异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.1415926535\n",
      "8979323846\n",
      "2643383279\n"
     ]
    }
   ],
   "source": [
    "#10.1 从文件中读取数据\n",
    "with open('pi_digit.txt') as file_object:\n",
    "    contents=file_object.read()\n",
    "    print(contents)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.1415926535\n",
      "\n",
      "8979323846\n",
      "\n",
      "2643383279\n"
     ]
    }
   ],
   "source": [
    "filename='pi_digit.txt'\n",
    "with open(filename) as file_object:\n",
    "    for line in file_object:\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "filename='pi_digit.txt'\n",
    "with open(filename) as file_object:\n",
    "    lines=file_object.readlines()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "3.141592653589793238462643383279\n",
      "32\n"
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "pi_string=''\n",
    "for line in lines:\n",
    "    pi_string+=line.rstrip()\n",
    "    \n",
    "print(pi_string)\n",
    "print(len(pi_string))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "Your birthday appears in the first million digits of pi\n"
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "## 圆周率值中包含你的生日吗\n",
    "filename='pi_million_digits.txt'\n",
    "\n",
    "with open(filename) as file_object:\n",
    "    lines=file_object.readlines()\n",
    "    pi_string=''\n",
    "    for line in lines:\n",
    "        pi_string+=line.strip()\n",
    "        \n",
    "birthday=input('Enter your birthday, in the form mmddyy: ')\n",
    "if birthday in pi_string:\n",
    "    print(\"Your birthday appears in the first million digits of pi\")\n",
    "else:\n",
    "    print(\"Your birthday does not appear in the first million digits of pi.\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "Give me two numbers, and I'll divide them.\n",
      "Enter 'q' to quit.\n",
      "You can't divide by 0!\n",
      "4.0\n",
      "6.0\n"
     ],
     "output_type": "stream"
    },
    {
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-db301775db9f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     10\u001b[0m     \u001b[0msecond_number\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Second number: '\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     11\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m         \u001b[0manswer\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfirst_number\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msecond_number\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     13\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mZeroDivisionError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     14\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"You can't divide by 0!\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: ''"
     ],
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: ''",
     "output_type": "error"
    }
   ],
   "source": [
    "##异常\n",
    "\n",
    "print(\"Give me two numbers, and I'll divide them.\")\n",
    "print(\"Enter 'q' to quit.\")\n",
    "\n",
    "while 1:\n",
    "    first_number=input(\"\\nFirst number: \")\n",
    "    if first_number == 'q':\n",
    "        break\n",
    "    second_number=input('Second number: ')\n",
    "    try:\n",
    "        answer=int(first_number)/int(second_number)\n",
    "    except ZeroDivisionError:\n",
    "        print(\"You can't divide by 0!\")\n",
    "    else:\n",
    "        print(answer)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "The file alice.txt has about 29461 words.\n"
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "filename='alice.txt'\n",
    "try:\n",
    "    with open(filename) as f_obj:\n",
    "        contents=f_obj.read()\n",
    "except FileNotFoundError:\n",
    "    msg=\"Sorry, the file \" + filename + \" does not exist.\"\n",
    "    print(msg)\n",
    "else:\n",
    "    words=contents.split()\n",
    "    num_words=len(words)\n",
    "    print(\"The file \" + filename + \" has about \" + str(num_words) + \" words.\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "## 存储数据\n",
    "import json\n",
    "numbers=[2,3,4,5,6,6]\n",
    "filename='numbers.json'\n",
    "with open(filename,'w') as f_obj:\n",
    "    json.dump(numbers,f_obj)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "[2, 3, 4, 5, 6, 6]\n"
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "filename='numbers.json'\n",
    "with open(filename) as f_obj:\n",
    "    numbers=json.load(f_obj)\n",
    "print(numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 11 测试函数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "Enter 'q' at any time to quit.\n",
      "/tNeatly formatted name: H H.\n",
      "/tNeatly formatted name: H .\n"
     ],
     "output_type": "stream"
    }
   ],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stderr",
     "text": [
      "E\n",
      "======================================================================\n",
      "ERROR: C:\\Users\\78186\\AppData\\Roaming\\jupyter\\runtime\\kernel-42924df6-3ed4-42f7-9e4d-376ccbc2e028 (unittest.loader._FailedTest)\n",
      "----------------------------------------------------------------------\n",
      "AttributeError: module '__main__' has no attribute 'C:\\Users\\78186\\AppData\\Roaming\\jupyter\\runtime\\kernel-42924df6-3ed4-42f7-9e4d-376ccbc2e028'\n",
      "\n",
      "----------------------------------------------------------------------\n",
      "Ran 1 test in 0.001s\n",
      "\n",
      "FAILED (errors=1)\n",
      "D:\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py:3304: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
      "  warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
     ],
     "output_type": "stream"
    },
    {
     "traceback": [
      "An exception has occurred, use %tb to see the full traceback.\n",
      "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m True\n"
     ],
     "ename": "SystemExit",
     "evalue": "True",
     "output_type": "error"
    }
   ],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}